
import { getWins ,getBanners} from '../../api/api.js';

const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    productList:[],
    parameter: {
      'navbar': '1',
      'return': '1',
      'title': '抽选公示',
    },
    navH: "",
    is_switch:false,
    where: {
      keyword: '',
      page: 1,
      limit: 10,
    },
    loadend:false,
    loading:false,
    loadTitle:'加载更多',
    userInfo:{},
    banners:[],
    prize_introduce:""
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({ 
      navH: app.globalData.navHeight
    });
    this.get_product_list();
    this.getBanners()

  },
  getBanners(){
    let that = this
    getBanners().then(res=>{
      that.setData({
        banners:res.data.fansland_banner,
        prize_introduce:res.data.prize_introduce
      })
    })
  },
   /**
   * 商品详情跳转
   */
  goDetail: function (e) {
    let item = e.currentTarget.dataset.item;
     wx.navigateTo({ url: `/pages/fanslond_detail/index?id=${item.id}` });
   },

  searchSubmit: function (e) {
    var that = this;
    this.setData({ ['where.keyword']: e.detail.value, loadend: false, ['where.page']: 1 })
    this.get_product_list(true);
  },
  
  get_product_list: function (isPage) {
    let that = this;
    if (that.data.loadend) return;
    if (that.data.loading) return;
    if (isPage === true) that.setData({ productList: [] });
    that.setData({ loading: true, loadTitle: '' });
    getWins(that.data.where).then(res=>{
      let list = res.data.data;
      let productList = app.SplitArray(list, that.data.productList);
      let loadend = list.length < that.data.where.limit;
      that.setData({
        loadend: loadend,
        loading: false,
        loadTitle: loadend ? '已全部加载' : '加载更多',
        productList: productList,
        ['where.page']: that.data.where.page + 1,
      });
    }).catch(err=>{
      that.setData({ loading: false, loadTitle: '加载更多' });
    });
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.setData({['where.page']:1,loadend:false,productList:[]});
    this.get_product_list();
    wx.stopPullDownRefresh();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    this.get_product_list();
  },
})